package com.ilop.sthome.common.command;

import android.util.Log;

import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.udp.GatewayUdpUtil;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.tools.ByteUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author skygge
 * @Date on 2020-02-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：公共指令（发送json指令）
 */
public class SiterCommand {

    private static final String TAG = SiterCommand.class.getSimpleName();

    public static final int EQUIPMENT_CONTROL = 1;
    public static final int INCREASE_EQUIPMENT = 2;
    public static final int REPLACE_EQUIPMENT = 3;
    public static final int DELETE_EQUIPMENT = 4;
    public static final int MODIFY_EQUIPMENT_NAME = 5;
    public static final int CHOOSE_SCENE = 6;
    public static final int CANCEL_INCREASE_EQUIPMENT = 7;
    public static final int INCREASE_AUTOMATION = 8;
    public static final int MODIFY_AUTOMATION = 9;
    public static final int DELETE_AUTOMATION = 10;
    public static final int SET_GATEWAY_INFO = 12;
    public static final int TIME_CHECK = 21;
    public static final int INCREASE_SCENE = 23;
    public static final int SYN_DEVICE_NAME = 24;
    public static final int UPLOAD_SCENE_INFO = 26;
    public static final int UPLOAD_SCENE_AUTO_INFO = 27;
    public static final int UPLOAD_CURRENT_SCENE = 28;
    public static final int SYN_DEVICE_STATUS = 29;
    public static final int SYN_AUTOMATION = 30;
    public static final int SYN_SCENE = 32;
    public static final int SCENE_HANDLE = 38;
    public static final int DELETE_SCENE = 39;
    public static final int MODEL_SWITCH_TIMER = 40;
    public static final int MODEL_TIMER_SYN = 41;
    public static final int MODEL_TIMER_DEL = 42;
    public static final int SEND_TIMEZONE = 251;
    public static final int ALARM_LIST_SYNC = 44;
    public static final int DELETE_GATEWAY_LIST = 46;
    public static final int SUBDEVICE_ALARM_LIST_SYNC = 47;
    public static final int DELETE_SUBDEVICE_ALARM_LIST = 49;
    public static final int MODIFY_SUB_ROOM = 53;
    public static final int SYN_ALL_DEVICE_STATUS = 54;

    //上行cmd值
    public static final int SEND_ACK = 11;
    public static final int UPLOAD_GATEWAY_INFO = 13;
    public static final int UPLOAD_DEVICE_NAME = 17;
    public static final int UPLOAD_DEVICE_STATUS = 19;
    public static final int CHECK_TIME_NOW = 22;
    public static final int UPLOAD_TIMER_INFO = 43;
    public static final int UPLOAD_ALARM_LOGS_INFO = 45;
    public static final int UPLOAD_SUB_DEVICE_ALARM_LOGS_INFO = 48;
    public static final int UPLOAD_ALL_DEVICE_STATUS = 55;
    public static final int UPLOAD_ALL_DEVICE_STATUS2 = 56;

    private String mIotId;
    private String mDeviceName;
    
    public SiterCommand(DeviceInfoBean device) {
        this.mIotId = device.getIotId();
        this.mDeviceName = device.getDeviceName();
    }

    private static String jsonToString(String code){
        try {
            JSONObject json = new JSONObject(code);
            return json.toString();
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /* *****************************************  设备部分  **************************************************/

    /**
     * 设备控制（1）
     * @param deviceId 设备ID
     * @param status2 设备状态
     * @return 下发命令
     */
    String equipControl(int deviceId, String status2){
        String device = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+EQUIPMENT_CONTROL+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \""+status2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+EQUIPMENT_CONTROL+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \""+status2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 添加设备（2）
     * @return 下发命令
     */
    String equipIncrease(String roomId){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+INCREASE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \"" + roomId + "\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode = "{ " +
                    "    \"iotId\": \"" + mIotId + "\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": " + mMsgId + ",  " +
                    "    \"CMD_CODE\": " + INCREASE_EQUIPMENT + ",  " +
                    "    \"rev_str1\": \"" + roomId + "\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 替换设备（3）
     * @param deviceId 设备ID
     * @param deviceType 设备类型
     * @return 下发命令
     */
    String equipReplace(int deviceId, String deviceType){
        String device = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+REPLACE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \""+deviceType+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+REPLACE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \""+deviceType+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 删除设备（4）
     * @param deviceId 设备ID
     * @return 下发命令
     */
    String equipDelete(int deviceId){
        String device = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+DELETE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+DELETE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 修改设备名字 （5）
     * @param deviceId 设备ID
     * @param newName 昵称
     * @return 下发命令
     */
    String modifyEquipmentName(int deviceId, String newName){
        String device = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+MODIFY_EQUIPMENT_NAME+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \""+newName+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+MODIFY_EQUIPMENT_NAME+",  " +
                    "    \"rev_str1\": \""+device+"\"," +
                    "    \"rev_str2\": \""+newName+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 查询或设置网关信息 (12)
     * @param str1 00:查询 01:设置USB功能 02:设置子设备离线
     * @param str2 (00:小夜灯，01:充电) 或者(00:推送 01:不推送)
     * @return code
     */
    String queryOrSetGatewayInfo(String str1, String str2){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SET_GATEWAY_INFO+",  " +
                    "    \"rev_str1\": \""+str1+"\"," +
                    "    \"rev_str2\": \""+str2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SET_GATEWAY_INFO+",  " +
                    "    \"rev_str1\": \""+str1+"\"," +
                    "    \"rev_str2\": \""+str2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 设备状态同步（29）
     * @param statusCRC 设备状态 对应的CRC校验码
     * @return 下发命令
     */
    String synDeviceStatus(String statusCRC){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SYN_DEVICE_STATUS+",  " +
                    "    \"rev_str1\": \""+statusCRC+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SYN_DEVICE_STATUS+",  " +
                    "    \"rev_str1\": \""+statusCRC+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 设备名称同步（24）
     * @param nameCRC 设备名称对应的CRC校验码
     * @return 下发命令
     */
    String synDeviceName(String nameCRC){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SYN_DEVICE_NAME+",  " +
                    "    \"rev_str1\": \""+nameCRC+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else{
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SYN_DEVICE_NAME+",  " +
                    "    \"rev_str1\": \""+nameCRC+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 修改设备房间（53）
     * @param deviceId device_ID
     * @param roomId 房间编号(1byte:”01”-“10”)
     * @return 下发命令
     */
    String modifySubDeviceRoom(int deviceId, String roomId){
        String deviceID = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+MODIFY_SUB_ROOM+",  " +
                    "    \"rev_str1\": \"" + deviceID + "\"," +
                    "    \"rev_str2\": \"" + roomId + "\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else{
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+MODIFY_SUB_ROOM+",  " +
                    "    \"rev_str1\": \"" + deviceID + "\"," +
                    "    \"rev_str2\": \"" + roomId + "\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 设备状态同步 新（54）
     * @param statusCRC 设备状态 对应的CRC校验码
     * @return 下发命令
     */
    String synAllDeviceStatus(String statusCRC, String zone){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SYN_ALL_DEVICE_STATUS+",  " +
                    "    \"rev_str1\": \""+statusCRC+"\"," +
                    "    \"rev_str2\": \""+ zone + "\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SYN_ALL_DEVICE_STATUS+",  " +
                    "    \"rev_str1\": \""+statusCRC+"\"," +
                    "    \"rev_str2\": \""+ zone + "\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /* *****************************************  场景部分  **************************************************/

    /**
     * 选择切换场景（6）
     * @param sid 场景Id
     * @return 服务json
     */
    String choseScene(int sid){
        String sceneId = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(sid));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+CHOOSE_SCENE+",  " +
                    "    \"rev_str1\": \""+sceneId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else{
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+CHOOSE_SCENE+",  " +
                    "    \"rev_str1\": \""+sceneId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 同步场景（32）
     * @param sceneCRC 所有场景CRC校验码，有空缺补0
     * @return 服务json
     */
    String synScene(String sceneCRC){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SYN_SCENE+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \""+sceneCRC+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+(SYN_SCENE)+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \""+sceneCRC+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 添加/修改场景（23）
     * @param scene 场景信息
     * @return 服务json
     */
    String increaseScene(String scene){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+INCREASE_SCENE+",  " +
                    "    \"rev_str1\": \""+scene+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+INCREASE_SCENE+",  " +
                    "    \"rev_str1\": \""+scene+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 删除场景（39）
     * @param sid 场景Id
     * @return 服务json
     */
    String deleteScene(int sid){
        String sceneId = ByteUtil.toHexString(sid);
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+DELETE_SCENE+",  " +
                    "    \"rev_str1\": \""+sceneId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+DELETE_SCENE+",  " +
                    "    \"rev_str1\": \""+sceneId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }


    /* *****************************************  自动化部分  **************************************************/

    /**
     * 同步自动化（30）
     * @param automation 自动化生效位
     * @param CRCPage 生效自动化CRC校验码 page1
     * @return 服务json
     */
    String synAutomation(String automation, String CRCPage){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SYN_AUTOMATION+",  " +
                    "    \"rev_str1\": \""+automation+"\"," +
                    "    \"rev_str2\": \""+CRCPage+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SYN_AUTOMATION+",  " +
                    "    \"rev_str1\": \""+automation+"\"," +
                    "    \"rev_str2\": \""+CRCPage+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 同步自动化（30）
     * @param automation 自动化生效位
     * @param CRCPage 生效自动化CRC校验码 page1
     * @param CRCPage2 生效自动化CRC校验码 page2
     * @return 服务json
     */
    String synAutomationWithPage(String automation, String CRCPage, String CRCPage2){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SYN_AUTOMATION+",  " +
                    "    \"rev_str1\": \""+automation+"\"," +
                    "    \"rev_str2\": \""+CRCPage+"\"," +
                    "    \"rev_str3\": \""+CRCPage2+"\"," +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SYN_AUTOMATION+",  " +
                    "    \"rev_str1\": \""+automation+"\"," +
                    "    \"rev_str2\": \""+CRCPage+"\"," +
                    "    \"rev_str3\": \""+CRCPage2+"\"," +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 添加自动化（8）
     * @param automation 自动化信息
     * @return 服务json
     */
    String increaseAutomation(String automation){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+INCREASE_AUTOMATION+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \""+automation+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+INCREASE_AUTOMATION+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \""+automation+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 修改自动化（9）
     * @param automation 自动化信息
     * @return 服务json
     */
    String updateAutomation(String automation){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+MODIFY_AUTOMATION+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \""+automation+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode = "{ " +
                    "    \"iotId\": \"" + mIotId + "\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": " + mMsgId + ",  " +
                    "    \"CMD_CODE\": " + MODIFY_AUTOMATION + ",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \"" + automation + "\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 删除自动化（10）
     * @param mid 自动化Id
     * @return 服务json
     */
    String deleteAutomation(int mid){
        String autoId = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(mid));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+DELETE_AUTOMATION+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+DELETE_AUTOMATION+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 点击执行（38）
     * @param mid 自动化编号
     * @return
     */
    public String autoClick(int mid){
        String autoId = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(mid));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if(GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SCENE_HANDLE+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else{
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SCENE_HANDLE+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }


    /* *****************************************  其他部分  **************************************************/

    /**
     * 取消配网 (7)
     * @return code
     */
    String cancelEquipIncrease(){
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+CANCEL_INCREASE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+CANCEL_INCREASE_EQUIPMENT+",  " +
                    "    \"rev_str1\": \"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 报警推送信息同步请求 (44)
     * @param page 第几页  “01”页为最新 每页10条共100条
     * @return code
     */
    String syncAlarmLogs(int page){
        String autoId = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(page));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+ALARM_LIST_SYNC+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode = "{ " +
                    "    \"iotId\": \"" + mIotId + "\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": " + mMsgId + ",  " +
                    "    \"CMD_CODE\": " + ALARM_LIST_SYNC + ",  " +
                    "    \"rev_str1\": \"" + autoId + "\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 报警推送信息删除 (46)
     * @param id ”第几条”  “00”-“64”:删除第几条记录  “FF”:清空
     * @return code
     */
    String deleteGatewayLog(int id){
        String autoId = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(id)).toUpperCase();
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+DELETE_GATEWAY_LIST+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+DELETE_GATEWAY_LIST+",  " +
                    "    \"rev_str1\": \""+autoId+"\"," +
                    "    \"rev_str2\": \"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }

        return jsonToString(groupCode);
    }

    /**
     * 子设备信息同步请求 (47)
     * @param page ”第几页” “01”页为最新 每页10条共100条
     * @param deviceId ”子设备ID” 例如“0001”
     * @return code
     */
    String syncSubAlarmLogs(int page, int deviceId){
        String de1 = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(page)).toUpperCase();
        String de2 = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+SUBDEVICE_ALARM_LIST_SYNC+",  " +
                    "    \"rev_str1\": \""+de1+"\"," +
                    "    \"rev_str2\": \""+de2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode ="{ " +
                    "    \"iotId\": \""+mIotId+"\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": "+ mMsgId +",  " +
                    "    \"CMD_CODE\": "+SUBDEVICE_ALARM_LIST_SYNC+",  " +
                    "    \"rev_str1\": \""+de1+"\"," +
                    "    \"rev_str2\": \""+de2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * 子设备信息删除 (49)
     * @param id ”第几条”  “00”-“64”:删除第几条记录  “FF”:清空
     * @param deviceId ”子设备ID” 例如“0001”
     * @return code
     */
    String deleteSubDeviceLog(int id, int deviceId){
        String de1 = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(id));
        String de2 = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(deviceId));
        int mMsgId = CommonId.getInstance().mMsgId++;
        String groupCode;
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            groupCode ="{ " +
                    "    \"action\": \"APP_SEND\",  " +
                    "    \"devID\": \""+mDeviceName+"\",  " +
                    "    \"msg\": { " +
                    "    \"msg_ID\": "+mMsgId+",  " +
                    "    \"CMD_CODE\": "+DELETE_SUBDEVICE_ALARM_LIST+",  " +
                    "    \"rev_str1\": \""+de1+"\"," +
                    "    \"rev_str2\": \""+de2+"\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }else {
            groupCode = "{ " +
                    "    \"iotId\": \"" + mIotId + "\",  " +
                    "    \"identifier\": \"data_revive\",  " +
                    "    \"args\": { " +
                    "    \"msg_ID\": " + mMsgId + ",  " +
                    "    \"CMD_CODE\": " + DELETE_SUBDEVICE_ALARM_LIST + ",  " +
                    "    \"rev_str1\": \"" + de1 + "\"," +
                    "    \"rev_str2\": \"" + de2 + "\"," +
                    "    \"rev_str3\": \"\"" +
                    "    } " +
                    "}";
        }
        return jsonToString(groupCode);
    }

    /**
     * udp应答
     * 251
     * @param
     * @return
     */
    public static String appAnswerOk(String deviceName, int cmd){
        String Code ="{ " +
                "    \"action\": \"APP_ACK\",  " +
                "    \"devID\": \""+deviceName+"\",  " +
                "    \"msg\": { " +
                "    \"msg_ID\": "+ CommonId.getInstance().mMsgId++ +",  " +
                "    \"CMD_CODE\": "+SEND_ACK+",  " +
                "    \"rev_str1\": \""+cmd+"\"," +
                "    \"rev_str2\": \"OK\"," +
                "    \"rev_str3\": \"\"" +
                "    } " +
                "}";
        return jsonToString(Code);
    }
}
